package com.example.zqh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zqh.mapper.CommentMapper;
import com.example.zqh.mapper.FoodMapper;
import com.example.zqh.mapper.UserMapper;
import com.example.zqh.model.RespBean;
import com.example.zqh.pojo.Comment;
import com.example.zqh.pojo.Food;
import com.example.zqh.pojo.Shop;
import com.example.zqh.mapper.ShopMapper;
import com.example.zqh.pojo.User;
import com.example.zqh.pojo.params.OneStringOneNumber;
import com.example.zqh.pojo.params.FourString;
import com.example.zqh.pojo.params.TwoInt;
import com.example.zqh.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zqh.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-09-10
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {


    @Autowired
    ShopMapper shopMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    FoodMapper foodMapper;




    /**
      * @Author: zqh
      * @Date:
      * @Description: 添加一个新商店
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean addANewStore(FourString threeString, Principal principal) {
        String name = principal.getName();
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", name));
        AssertUtil.isTrue(user == null,"参数传递错误");

        //店铺名不能重复
        Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("name", threeString.getName()));
        AssertUtil.isTrue(shop != null,"该店铺名字已经存在");

        //一个用户只能创建一个店铺
        Shop shop2 = shopMapper.selectOne(new QueryWrapper<Shop>().eq("user_id", user.getId()));
        System.out.println(shop2);
        AssertUtil.isTrue(shop2 != null,"只能创建一个店铺");

        Shop shop1 = new Shop();
        shop1.setName(threeString.getName())
                .setUserId(user.getId())
                //一开始默认星级为1星
                .setStart(1)
                .setLocation(threeString.getLocation())
                .setPic(threeString.getPic())
                .setIntro(threeString.getIntro());

        int insert = shopMapper.insert(shop1);
        AssertUtil.isTrue(insert != 1, "创建店铺失败");

        return RespBean.success("创建店铺成功");
    }
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 修改店铺名
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyTheStoreName(OneStringOneNumber oneStringOneNumber) {
        Shop shop = shopMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(shop == null,"参数传递错误");
        //判断新的店铺名是否重复
        Shop shop1 = shopMapper.selectOne(new QueryWrapper<Shop>().eq("name", oneStringOneNumber.getText()));
        AssertUtil.isTrue(shop1 != null,"该店铺名已存在");
        shop.setName(oneStringOneNumber.getText());
        int i = shopMapper.updateById(shop);
        AssertUtil.isTrue(i != 1,"修改店铺名失败");
        return RespBean.success("修改店铺名成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改店铺所在位置
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyStoreLocation(OneStringOneNumber oneStringOneNumber) {
        Shop shop = shopMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(shop == null,"参数传递错误");
        shop.setLocation(oneStringOneNumber.getText());
        int i = shopMapper.updateById(shop);
        AssertUtil.isTrue(i != 1,"修改店铺位置失败");
        return RespBean.success("修改店铺位置成功");
    }
    
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 修改店铺简介
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyTheStoreProfile(OneStringOneNumber oneStringOneNumber) {
        Shop shop = shopMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(shop == null,"参数传递错误");
        shop.setIntro(oneStringOneNumber.getText());
        int i = shopMapper.updateById(shop);
        AssertUtil.isTrue(i != 1,"修改店铺简介失败");
        return RespBean.success("修改店铺简介成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改店铺星级，管理员接口
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyStoreStarRating(TwoInt twoInt) {
        Shop shop = shopMapper.selectById(twoInt.getId());
        AssertUtil.isTrue(shop == null,"参数传递错误");
        shop.setStart(twoInt.getPrice());
        int i = shopMapper.updateById(shop);
        AssertUtil.isTrue(i != 1,"修改店铺星级失败");
        return RespBean.success("修改店铺星级成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 店铺删除接口，需要管理员权限
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean storeDeleteInterface(Integer id) {
        //删除该店铺的所有食物
        int shop_id = foodMapper.delete(new QueryWrapper<Food>().eq("shop_id", id));
        AssertUtil.isTrue(shop_id != 1,"店铺删除失败");
        //删除该店铺的所有评论
        int shop_id1 = commentMapper.delete(new QueryWrapper<Comment>().eq("shop_id", id));
        AssertUtil.isTrue(shop_id1 != 1,"删除店铺失败");

        int i = shopMapper.deleteById(id);
        AssertUtil.isTrue(i != 1,"删除店铺失败");
        return RespBean.success("删除店铺成功");
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改店铺图片
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean modifyTheStoreImage(OneStringOneNumber oneStringOneNumber) {
        Shop shop = shopMapper.selectById(oneStringOneNumber.getId());
        AssertUtil.isTrue(shop == null,"参数传递错误");
        shop.setPic(oneStringOneNumber.getText());
        int i = shopMapper.updateById(shop);
        AssertUtil.isTrue(i != 1, "修改店铺图片失败");
        return RespBean.success("修改该店铺图片成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 获取所有商店的数量
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getAllSp() {
        Integer shops = shopMapper.getAllShops();
        return RespBean.success("查询成功",shops);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 分页查询店铺
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean pagingQueryStore(Integer page, Integer size) {
        List<Shop> shops = shopMapper.storePagingQuery((page - 1) * size, size);
        return RespBean.success("查询成功",shops);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过店铺id查询店铺
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean searchStoreByStoreId(Integer id) {
        Shop shop = shopMapper.selectById(id);
        return RespBean.success("查询成功",shop);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过星级查询店铺，分页
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean searchStoresByStarRating(Integer start, Integer page, Integer size) {
        List<Shop> shops = shopMapper.starQueryStore(start, (page - 1) * size, size);
        return RespBean.success("查询成功",shops);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过星级查询不同商店的数量
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getNumberByStart(Integer start) {
        Integer number = shopMapper.getShopNumberByStart(start);
        return RespBean.success("查询成功",number);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 管理员修改商店信息接口
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean adminModifyStoreInformationInterface(Shop shop) {
        //判断修改的商店名称在数据库中是否有重复
        Shop shop1 = shopMapper.selectById(shop.getId());

//        AssertUtil.isTrue(shop.getName() != shop1.getName() && shop2 != null,"该店铺名已存在，无法修改此店铺名");
        //当修改了店铺名时
        if (!shop.getName().equals(shop1.getName())){
            Shop shop2 = shopMapper.selectOne(new QueryWrapper<Shop>().eq("name", shop.getName()));
            AssertUtil.isTrue(shop2 != null,"该店铺名已存在，无法修改成此店铺名");
        }

        int i = shopMapper.updateById(shop);
        AssertUtil.isTrue(i != 1,"修改店铺信息失败");
        return RespBean.success("修改店铺信息成功");
    }
}
